package com.greenleaf.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/uploadImage")
public class ImageUploadController {
@RequestMapping("/upload")
@ResponseBody
//http://localhost:8080/Greenleaf/uploadImage/upload
  public Object uploadImage(MultipartFile file,HttpServletRequest request) {
	  Map<String, Object> rnMap=new HashMap<String, Object>();
	  try { 
	   //获取文件名字
	    String filename = file.getOriginalFilename();
		String uploadOir = request.getServletContext().getRealPath("/images");
		//System.out.println(uploadOir);//打印上传图片所在路径
		File targetFile = new File(uploadOir, filename);
		file.transferTo(targetFile);
		rnMap.put("success", true);
		rnMap.put("message","上传成功" );
		rnMap.put("remark", filename);
	} catch (Exception e) {
		rnMap.put("success", false);
		rnMap.put("message","上传失败" );
		rnMap.put("remark", "格式不正确");
		e.printStackTrace();
	}
	  return rnMap;
	  
  }

}
